// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

//const i18n = require('i18n');

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.ProgressBar)
    progressBar:cc.ProgressBar;

    @property(cc.Label)
    progressTips:cc.Label;

    @property(cc.Node)
    loadBg:cc.Node;

    _urls=[];

    resource=null;

   progress:number=0;

   completedCount:number;

   totalCount:number=0;
    // LIFE-CYCLE CALLBACKS:

  onLoad () {

    this._urls = [
        cc.url.raw("resources/audio/ding.wav"),
        cc.url.raw("resources/audio/cheering.wav"),
        cc.url.raw("resources/audio/music_logo.mp3"),
        cc.url.raw("resources/test assets/audio.mp3"),
        cc.url.raw("resources/loadingBar/font.png"),
        cc.url.raw("resources/loadingBar/mikado_outline_shadow.png"),
        cc.url.raw("resources/loadingBar/enligsh-chinese.png")
    ];
    this.resource = null;

    //进度条的进度（0-1）
    this.progressBar.progress = 0;

    this._clearAll();

    this.progressTips.textKey="cases/05_scripting/10_loadingBar/loadingBar.fire.18";

    this.node.on(cc.Node.EventType.TOUCH_START,()=>{

        if(this.resource){return;}

        cc.loader.load(this._urls,this._progressCallback.bind(this),this._completeCallback.bind(this));
    },this);

  }

  //清空所有下载的资源
  _clearAll () {
    for (var i = 0; i < this._urls.length; ++i) {
        var url = this._urls[i];
        cc.loader.release(url);
    }
}

_progressCallback (completedCount, totalCount, res) {

    this.progress = completedCount / totalCount;

    this.resource = res;


    this.completedCount = completedCount;

    this.totalCount = totalCount;
}

    _completeCallback (error, res) {

}


     update (dt:number) {

        //资源不存在，则返回
        if(this.resource===null){return;}

        let progressTemp=this.progressBar.progress;
        
        //下载完
        if(progressTemp>=1){

            this.loadBg.active=false;

            this.progressBar.node.active=false;

            //该组件停用
            this.enabled=false;

            return;
        }

        if(progressTemp<this.progress){

            //progress其实是随便加的，与实际加载没关系
            progressTemp+=dt;
        }

        this.progressBar.progress=progressTemp;
        
     }
}
